#include "CAUBuilder.h"


CAUBuilder::CAUBuilder(unsigned int nAudioPID, unsigned int nVideoPID)
{
	m_nAudioPID = nAudioPID;
	m_nVideoPID = nVideoPID;
}

CAUBuilder::~CAUBuilder(void)
{
	for(unsigned int j=0; j<m_audioAus.size(); j++)
		delete m_audioAus.at(j);
	for(unsigned int j=0; j<m_videoAus.size(); j++)
		delete m_videoAus.at(j);
}

void CAUBuilder::BuildAu(IPESBuilder* cPesBuilder){
// building video AUs of the video PESs list
	_buildVideoAu(cPesBuilder);
// building audio AUs of the audio PESs list
	_buildAudioAu(cPesBuilder);  
}


void CAUBuilder::_buildAudioAu(IPESBuilder* cPesBuilder){

	std::vector<CPES*>* pPesList = cPesBuilder->getPESPackets(m_nAudioPID);
	if (NULL == pPesList)
		return;

	for(unsigned int i=0; i<pPesList->size(); i++)
	{
		CDynamicArray* cAu = NULL;
		CPES* cPes = pPesList->at(i);
		int nPayloadSize = cPes->getPayloadLength();
        //retrieves the audio PES payload
		const char* cPayload =   cPes->getPayload(); 

		/*int count=0;
		int temp=0;
		char* start_from=NULL;
		while ((count+1)< nPayloadSize){
		if (cPayload[count]==0xb && cPayload[count+1] == 0x77)
		{
		if (cAu != NULL)
		{
		start_from=cPayload+(count-temp);
		cAu->copyToArray(start_from, temp);
		m_aus.push_back(cAu);
		}
		cAu = new CDynamicArray();
		temp = 0;
		}
		temp = temp+1;
		count = count+1;
		}
		start_from = cPayload+(count-temp);
		cAu->copyToArray(start_from, temp+1);*/
        //creates new AU
		cAu = new CDynamicArray(nPayloadSize);
        //copying the PES payload to the new AU array
		cAu->copyToArray(cPayload, nPayloadSize); 
        // adds the new AU to the audio AU list
		m_audioAus.push_back(cAu); 

		delete cPes;
	}
	pPesList->clear();
}


void

CAUBuilder::_buildVideoAu(IPESBuilder* cPesBuilder)
{
	std::vector<CPES*>* pPesList = cPesBuilder->getPESPackets(m_nVideoPID);
	if (NULL == pPesList)
		return;

	CDynamicArray* vidAU = NULL;

	for(unsigned int i=0; i<pPesList->size(); i++)
	{
		CPES* cPes = pPesList->at(i);
		
		int nPayloadSize = cPes->getPayloadLength();
        //retrieves the video PES payload
		const char* cPayload =   cPes->getPayload(); 
	
		if (nPayloadSize <= 0){
			delete cPes;
			continue;
		}

		/*int count=0;
		int temp=0;
		char* start_from=NULL;
		while ((count+2)< nPayloadSize)
		{
			if ((cPayload[count]==0) && (cPayload[count+1]==0) && (cPayload[count+2]==1))
			{
				if (vidAU != NULL)
				{
					start_from=cPayload+(count-temp);
					vidAU->copyToArray(start_from,temp);
					m_videoAus.push_back(vidAU);
				}
				vidAU =	new CDynamicArray();
				temp=0;
			}
			temp=temp+1;
			count=count+1;
		}
		start_from=cPayload+(count-temp);
		vidAU->copyToArray(start_from,temp+2);*/

		// creates new AU 
		vidAU = new CDynamicArray(nPayloadSize);
        // copying the PES payload to the new AU array
		vidAU->copyToArray(cPayload, nPayloadSize); 
        // add the new AU to the video AU list
		m_videoAus.push_back(vidAU); 

		delete cPes;
	}

	pPesList->clear();
}
